package com.minivision.sdktestdemo.ui.view;

import android.app.Activity;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.minivision.sdktestdemo.R;
import com.minivision.sdktestdemo.bean.MusicBean;
import com.minivision.sdktestdemo.event.DataModificationEvent;
import com.minivision.sdktestdemo.event.PlayMusicEvent;
import com.minivision.sdktestdemo.model.MusicProvider;
import com.minivision.sdktestdemo.model.Provider;
import com.minivision.sdktestdemo.utils.LoadingWindowUtils;
import com.minivision.sdktestdemo.utils.RingConfigUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by devtang on 2016/5/13.
 */
public class MusicCtrlPopContentView extends MusicCtrlPopContentViewImpl implements PopClickItemFactory.OnPlayListener,
        PopClickItemFactory.OnDeleteListener, PopClickItemFactory.OnScanMusicListener,
        PopClickItemFactory.OnSetRingListener, PlayBeanContentView {

    private ExecutorService mExecutor;

    private PlayMusicEvent mEvent;


    public MusicCtrlPopContentView(Activity context) {
        super(context);
        mExecutor = Executors.newSingleThreadExecutor();
    }
//    @Override
//    public int getMaxPopWindowHeight() {
//        int size = (int) (72 + getPopClickList().size() * mContext.getResources().getDimension(R.dimen.pop_item_size));
//        size = Math.min(768, size);
//        return size;
//    }

    private MusicBean mBean;

    @Override
    protected List<PopClickItemFactory.PopClickItem> getPopClickList() {
        if (getClickList() == null) {
            addItemToList(PopClickItemFactory.getPlayItem(this, this));

            addItemToList(PopClickItemFactory.getDeleteItem(this, this));

            addItemToList(PopClickItemFactory.getRingChangedItem(this, this));

            addItemToList(PopClickItemFactory.getScanMusicItem(this, this));
        }
        return getClickList();
    }

    @Override
    public void onDelete(final boolean isDeletedFlag) {
        LoadingWindowUtils.showProgressBack(getContext(), true);
        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                DataModificationEvent mModificationEvent = new DataModificationEvent(new ArrayList<MusicBean>(), DataModificationEvent.ACTION_DELETE);
                mModificationEvent.mAction = DataModificationEvent.ACTION_DELETE;
                MusicProvider provider = MusicProvider.getInstance(getContext());
                final int res = provider.remove(mBean, isDeletedFlag);
                Provider.adjustGroup(getContext(), mBean);
                mModificationEvent.mBeans.add(mBean);
                getContext().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        LoadingWindowUtils.showProgressBack(getContext(), false);
                        if (res == -1) {
                            Toast.makeText(getContext(), "删除失败", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getContext(), "删除成功", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                EventBus.getDefault().post(mModificationEvent);
            }
        });
    }

    @Override
    public void onPlay() {
        if (mList != null) {
            DataModificationEvent event = new DataModificationEvent(mList, DataModificationEvent.ACTION_GROUP_PLAY);
            EventBus.getDefault().post(event);
        } else {
            mEvent.mStatus = PlayMusicEvent.PLAY;
            EventBus.getDefault().post(mEvent);
        }
    }

    @Override
    public void onScanMusic() {
        PopupView popupView = new PopupView(getContext(), new MusicInfoContentView(getContext()));
        popupView.show(null, mBean);
    }

    @Override
    public void onSetChanged() {
        final PopupWindow popupWindow = new PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setContentView(getContext().getLayoutInflater().inflate(R.layout.layout_update, null));
        popupWindow.showAtLocation(getContext().findViewById(R.id.layout_main), Gravity.CENTER, 0, 0);
        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                RingConfigUtils.setMusicAsRingtones(getContext(), mBean.getmPath());
                getContext().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        popupWindow.dismiss();
                        Toast.makeText(getContext(), "设置铃声成功", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    public void setPlayBean(MusicBean bean) {
        mBean = bean;
        mEvent = new PlayMusicEvent(bean, PlayMusicEvent.EMPTY);
        setTitle("歌曲:" + bean.getmMusicName());
    }

    private List<MusicBean> mList;

    @Override
    public void setPlayBean(List<MusicBean> list, MusicBean bean) {
        setPlayBean(bean);
        mList = list;
    }
}
